package com.sgb.kjwl.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.chuanglan.shanyan_sdk.OneKeyLoginManager;
import com.chuanglan.shanyan_sdk.listener.ShanYanCustomInterface;
import com.chuanglan.shanyan_sdk.tool.ShanYanUIConfig;
import com.sgb.kjwl.R;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;

/**
 * 一键登录界面设置
 */
public class ConfigUtils {
    /**
     * 闪验三网运营商授权页配置类
     *
     * @param context
     * @return
     */
    //沉浸式竖屏样式
    public static ShanYanUIConfig   getCJSConfig(final Context context) {
        Resources resources = context.getResources();
        LayoutInflater inflater = LayoutInflater.from(context);
        /************************************************自定义控件**************************************************************/

        Drawable authNavHidden = resources.getDrawable(R.color.color_main_bg);
        Drawable logoImgPath = resources.getDrawable(R.mipmap.ic_launcher);
        Drawable logBtnImgPath = resources.getDrawable(R.drawable.bg_shap_color_change_2corner);
        Drawable back = resources.getDrawable(R.drawable.back_black);

        Drawable uncheckedImgPath = resources.getDrawable(R.drawable.icon_unselect_change_people);
        Drawable checkedImgPath = resources.getDrawable(R.drawable.icon_selected_change_people);


        //logoImgPath自定义logo
        RelativeLayout view_logo = (RelativeLayout) inflater.inflate(R.layout.shanyan_logo_layout, null);
        RelativeLayout.LayoutParams mLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        view_logo.setLayoutParams(mLayoutParams);

        Bitmap bitmap = convertViewToBitmap(view_logo);
        logoImgPath = new BitmapDrawable(bitmap);


        Button close = new Button(context);
        close.setBackgroundResource(context.getResources().getIdentifier("close", "drawable", context.getPackageName()));
        RelativeLayout.LayoutParams mLayoutParamsClose = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        mLayoutParamsClose.setMargins(0, AbScreenUtils.dp2px(context, 15), AbScreenUtils.dp2px(context, 15), 0);
        mLayoutParamsClose.width = AbScreenUtils.dp2px(context, 20);
        mLayoutParamsClose.height = AbScreenUtils.dp2px(context, 20);
        mLayoutParamsClose.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        close.setLayoutParams(mLayoutParamsClose);

        //使用该手机号登录
//        TextView tip = new TextView(context);
//        tip.setText("使用该手机号登录");
//        tip.setTextColor(0xff888888);
//        tip.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
//        RelativeLayout.LayoutParams mLayoutParamsTip = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//        mLayoutParamsTip.setMargins(AbScreenUtils.dp2px(context, 80), AbScreenUtils.dp2px(context, 150), 0, 0);
//        mLayoutParamsTip.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
//        tip.setLayoutParams(mLayoutParamsTip);

         //其他方式登录
        RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.relative_item_view, null);
        RelativeLayout.LayoutParams layoutParamsOther = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParamsOther.setMargins(0, AbScreenUtils.dp2px(context, 350), 0, 0);
        layoutParamsOther.addRule(RelativeLayout.CENTER_HORIZONTAL);
        relativeLayout.setLayoutParams(layoutParamsOther);

        RelativeLayout rl = relativeLayout.findViewById(R.id.relative);
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                VerificationCodeLoginActivity.start(context, false);//修改登录页面3
                //销毁授权页
                OneKeyLoginManager.getInstance().finishAuthActivity();
            }
        });
        /*ImageView weixin = relativeLayout.findViewById(R.id.weixin);
        ImageView qq = relativeLayout.findViewById(R.id.qq);
        ImageView weibo = relativeLayout.findViewById(R.id.weibo);*/
        /*weixin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "微信登录", Toast.LENGTH_SHORT).show();
            }
        });
        qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "qq登录", Toast.LENGTH_SHORT).show();

            }
        });
        weibo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "微博登录", Toast.LENGTH_SHORT).show();

            }
        });*/


        /****************************************************设置授权页*********************************************************/
        ShanYanUIConfig uiConfig = new ShanYanUIConfig.Builder()
                //授权页导航栏：
                .setNavColor(Color.parseColor("#ffffff"))  //设置导航栏颜色
                .setNavText("")  //设置导航栏标题文字
                .setNavTextColor(0xff080808) //设置标题栏文字颜色
                .setAuthNavHidden(false)
                .setAuthBGImgPath(authNavHidden)
                .setAuthNavTransparent(true)
                .setNavReturnImgHidden(false)
                .setNavReturnImgPath(back)
                .setNavReturnBtnWidth(20)
                .setNavReturnBtnHeight(20)
                .setNavReturnBtnOffsetX(10)
                .setNavReturnBtnOffsetY(0)

                //授权页logo（logo的层级在次底层，仅次于自定义控件）
                .setLogoImgPath(logoImgPath)  //设置logo图片
                .setLogoWidth(AbScreenUtils.getScreenWidth(context, true))   //设置logo宽度
                .setLogoHeight(65)   //设置logo高度
                .setLogoOffsetY(30)  //设置logo相对于标题栏下边缘y偏移
                .setLogoHidden(false)   //是否隐藏logo
                .setLogoOffsetX(-40)

                //授权页号码栏：
                .setNumberColor(0xff000000)  //设置手机号码字体颜色
                .setNumFieldOffsetY(185)    //设置号码栏相对于标题栏下边缘y偏移
                .setNumFieldWidth(AbScreenUtils.getScreenWidth(context, true))
                .setNumberSize(39)
                .setNumFieldOffsetX(0)

                //授权页登录按钮：
                .setLogBtnText("一键登录")  //设置登录按钮文字
                .setLogBtnTextColor(0xffffffff)   //设置登录按钮文字颜色
                .setLogBtnImgPath(logBtnImgPath)   //设置登录按钮图片
                .setLogBtnOffsetY(290)   //设置登录按钮相对于标题栏下边缘y偏移
                .setLogBtnTextSize(15)
                .setLogBtnHeight(40)
                .setLogBtnWidth(AbScreenUtils.getScreenWidth(context, true) - 60)

                //授权页隐私栏：
                .setAppPrivacyOne("经营帮用户协议", Constants.LICENCE_URL)  //设置开发者隐私条款1名称和URL(名称，url)
//                .setAppPrivacyTwo("用户服务条款", "https://www.253.com")  //设置开发者隐私条款2名称和URL(名称，url)
                .setAppPrivacyColor(0xff444444, 0xff0085d0)   //	设置隐私条款名称颜色(基础文字颜色，协议文字颜色)
                .setPrivacyOffsetBottomY(30)//设置隐私条款相对于屏幕下边缘y偏
                .setUncheckedImgPath(uncheckedImgPath)
                .setCheckedImgPath(checkedImgPath)
                .setPrivacyState(true) // 用户协议是否默认选中

                //授权页slogan：
                .setSloganTextColor(0xff999999)  //设置slogan文字颜色
                .setSloganOffsetY(235)  //设置slogan相对于标题栏下边缘y偏移
                .setSloganOffsetX(85)


                // 添加自定义控件:
                //其他方式登录及监听，可以不写
                .addCustomView(close, true, false, new ShanYanCustomInterface() {
                    @Override
                    public void onClick(Context context, View view) {
                        Toast.makeText(context, "退出页面", Toast.LENGTH_SHORT).show();
                    }
                })
//                .addCustomView(tip, false, false, new ShanYanCustomInterface() {
//                    @Override
//                    public void onClick(Context context, View view) {
//
//                    }
//                })
                .addCustomView(relativeLayout, false, false, null)
                //标题栏下划线，可以不写
                .build();
        return uiConfig;

    }

    private static Bitmap convertViewToBitmap(View view) {
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());  //根据字符串的长度显示view的宽度
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }

}
